/*
Ext.namespace('Verbum.ext');
Verbum.ext.EditorColumnModel = function(config){
   Verbum.ext.EditorColumnModel.superclass.constructor.call(config);
};

Ext.extend(Verbum.ext.EditorColumnModel, Ext.grid.ColumnModel, {

})
*/

function preferencesRenderer(value, meta, record, rowIndex, colIndex, store) {
	switch (record.get('type')) {
		case 'bool':
			if (value=="true") {
				return _('Enabled');
			}
			return _('Disabled');
			break;
		case 'number':
			return "<span style=\"text-align: right\">"+value+"</span>";
			break;
		default:
			return value;
	}
}
			
MyDesktop.Preferences = Ext.extend(Ext.app.Module, {
	
    init : function(){
        /*
        this.launcher = {
            text: _('Preferences'),
            iconCls:'definitions',
            handler : this.createWindow,
            windowId : 'PrefMan',
            scope: this
        }*/
    },

    createWindow : function(src){
        var desktop = this.app.getDesktop();
        var winname = 'PrefMan';
        var win = desktop.getWindow(winname);

        if(!win){
			var boolEditor = new Ext.form.ComboBox({
				store: [['true',_('Enabled')],['false',_('Disabled')]],
				mode: 'local',
				triggerAction: 'all',
				editable: false	
			});

			var store = new Ext.data.Store({
	        	pruneModifiedRecords: true,
				reader: new Ext.data.JsonReader({
					root: 'results',
					id: 'id'
					},[
						{name: 'text'},
						{name: 'value'},
						{name: 'type'}
				]),
				proxy: new Ext.data.HttpProxy({
					url: getMainUrl(),
					method: 'POST'
				}),
				baseParams: {action: 'preferences'},
				autoLoad: false
	        });
		        
			var colModel = new Ext.grid.ColumnModel({
			editors: {
				'text': new Ext.grid.GridEditor(new Ext.form.TextField({})),
				'bool': new Ext.grid.GridEditor(boolEditor)	
			},
			getCellEditor: function(colIndex, rowIndex) {
				var rec = this.store.getAt(rowIndex);
				return this.editors[rec.get('type')];
			},
			columns: [
				{
					header: _('Option'),
					dataIndex: 'text',
					sortable: true,
					hideable: false,
					width: 300
				},{
					header: _('Value'),
					dataIndex: 'value',
					sortable: false,
					editable: true,
					hideable: false,
					renderer: preferencesRenderer
				}
			 ],
			store: store
			});

			var grid = new Ext.grid.EditorGridPanel({
				autoExpandColumn: 1,
		        store: store,
		        clicksToEdit:1,
		        cm: colModel,
		        viewConfig: {
		        	autoFill: true
		        }
			});

			grid.on({
				'afteredit': {
					fn: function(e) {
						e.grid.getStore().commitChanges();
					}
				}				
			});

			grid.getStore().on({
				'update': {
					fn: function(store,record,op) {
						if (Ext.data.Record.COMMIT==op) {
							Verbum.Util.request({
					            params: { 
					               action: "preferences", 
					               id:  record.id,
					               value: record.get('value'),
					               type: record.get('type'),
					               op: 'save'
					              }											
							});
							
						}
					}
				}	
			});
		
            win = desktop.createWindow({
				id: winname,
				title: _('Preferences'),
				closable:true,
				width:450,
				height:350,
				plain:true,
				layout: 'fit',
				iconCls: 'settings',
				items: grid
            });

            store.load();
        }
        win.show();
    },
	display : function(){
        this.createWindow();
	}
});
